{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 深度优先遍历二叉树\n",
    "\n",
    "深度优先搜索算法（英语：Depth-First-Search，DFS）是一种用于遍历或搜索树或图的算法。\n",
    "\n",
    "沿着树的深度遍历树的节点，尽可能深的搜索树的分支。当节点v的所在边都己被探寻过，搜索将回溯到发现节点v的那条边的起始节点。\n",
    "\n",
    "这一过程一直进行到已发现从源节点可达的所有节点为止。\n",
    "\n",
    "如果还存在未被发现的节点，则选择其中一个作为源节点并重复以上过程，整个进程反复进行直到所有节点都被访问为止。\n",
    "\n",
    "\n",
    "<img src=\"./deep_visit_tree.png\" style=\"margin-left:0px; width:300px;\"/>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Node(object):\n",
    "    def __init__(self, data, left=None, right=None):\n",
    "        self.data = data\n",
    "        self.left = left\n",
    "        self.right = right"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "tree = Node(\n",
    "    1, \n",
    "    Node(\n",
    "        2, \n",
    "        Node(4),\n",
    "        Node(5,Node(7), Node(8))),\n",
    "    Node(3, \n",
    "         Node(6))\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1、使用栈stack实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def deep_visit(root):\n",
    "    \"\"\"深度遍历二叉树\"\"\"\n",
    "    if not root: \n",
    "        return\n",
    "    stack = [root]\n",
    "    while stack:\n",
    "        # list.pop默认移除最后一个元素\n",
    "        current = stack.pop()\n",
    "        print(current.data, end=',')\n",
    "        \n",
    "        if current.right:\n",
    "            stack.append(current.right)\n",
    "            \n",
    "        if current.left:\n",
    "            stack.append(current.left)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1,2,4,5,7,8,3,6,"
     ]
    }
   ],
   "source": [
    "deep_visit(tree)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2、使用递归实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def deep_visit(root):\n",
    "    \"\"\"深度遍历二叉树\"\"\"\n",
    "    if not root: return\n",
    "    print(root.data, end=\",\")\n",
    "    deep_visit(root.left)\n",
    "    deep_visit(root.right)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "deep_visit(tree)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
